package com.wtpm.exam.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fwk.spbt.annotation.NoTokenCheckAnno;
import com.fwk.spbt.constant.SpbtRspEnum;
import com.fwk.spbt.controller.LoginController;
import com.fwk.spbt.exception.BsException;
import com.fwk.spbt.vo.RequestVO;
import com.fwk.spbt.vo.ResponeVO;
import com.wtpm.exam.vo.ExamUserVO;
import com.wtpm.sys.vo.WTPMUserVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/exam/login")
@Api(tags = { "exam->登陆" })
public class ExamLoginController extends LoginController {

	@CrossOrigin
	@ApiOperation(value = "考试用户登陆")
	@RequestMapping(value = "/in", method = { RequestMethod.POST })
	@NoTokenCheckAnno
	public ResponeVO<String> login(@RequestBody ExamUserVO reqDTO) {
		if (!checkVerifycode(reqDTO.getVsKey(), reqDTO.getVsCode()))
			throw new BsException(SpbtRspEnum.SPBT_00004, reqDTO.getVsKey());

		WTPMUserVO user = new WTPMUserVO();
		user.setUserId(reqDTO.getPhone());
		user.setUserName(reqDTO.getName());
		String token = redisCacheUser(user);
		return response(token);
	}

	@ApiOperation(value = "考试用户登出")
	@RequestMapping(value = "/out", method = { RequestMethod.POST })
	public ResponeVO<String> loginout(@RequestBody RequestVO reqVO) {
		redisDelteUser(reqVO.getToken());
		return response();
	}

	@ApiOperation(value = "考试用户验证码")
	@RequestMapping(value = "/verifycode", method = { RequestMethod.POST })
	@NoTokenCheckAnno
	public void verifycode(HttpServletRequest request, HttpServletResponse response) {
		verifycode(response);
	}

}
